﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScriptCompare
{
    public class OrderedLineCollection : LineCollectionBase
    {
        public OrderedLineCollection(IEnumerable<ScriptLine> source) : base(source) { }

        #region ILineCollection Members

        public ILineCollection CreateBlank()
        {
            // Get the number of lines in this collection
            int lines = this._lines.Count;

            // Create a list to hold the blank lines
            ScriptLine[] blanks = new ScriptLine[lines];

            // Create a new blank line and add it to the proper location
            for (int itr = 0; itr < lines; itr++)
            {
                blanks[itr] = new ScriptLine("", itr);
            }

            // Return the new Line Collection
            return new OrderedLineCollection(blanks);
        }
      
        public bool MergeCompare(ILineCollection compareTo)
        {
            // Make sure the comparison target is an ordered line collection 
            if (!(compareTo is OrderedLineCollection))
            {
                throw new InvalidOperationException("An OrderedLineCollection can only be compared to another OrderedLineCollection.");
            }

            // Setup some aliases
            IList<ScriptLine> allLinesLeft = this.GetComparableLines();
            IList<ScriptLine> allLinesRight = compareTo.GetComparableLines();

            // Number of lines
            int cntLinesLeft = allLinesLeft.Count;
            int cntLinesRight = allLinesRight.Count;

            // If the line count is different, immediately mark as different
            if (cntLinesLeft != cntLinesRight)
            {
                return (false);
            }
            else    // Go through each line and compare
            {
                // Go through each line
                for (int i = 0; i < cntLinesLeft; i++)
                {
                    // Read the lines from left/right, if the exist
                    ScriptLine lineLeft = allLinesLeft[i];
                    ScriptLine lineRight = allLinesRight[i];

                    // Check that they are the same
                    if (lineLeft.Matches(lineRight))
                        continue;
                    else
                        return false;
                }

            }

            // If we made it this far, the line collections are the same
            return (true);
        }

        #endregion
    }
}
